﻿using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
using Microsoft.Win32;

namespace WallpaperDeleter
{
    class TrayApplicationContext : ApplicationContext
    {
        private NotifyIcon notifyIcon;
        
        public TrayApplicationContext()
        {
            configureTrayIcon();
        }

        /// <summary>
        /// Creates a container for tray icon, creates notifyIcon, sets notifyIcon parameters
        /// creates a context menu for notifyIcon, and adds two ToolStripMenuItems - one
        /// to delete the Wallpaper, the other to exit the program.
        /// </summary>
        private void configureTrayIcon()
        {
            //Instantiate the component Module to hold everything
            System.ComponentModel.IContainer components = new System.ComponentModel.Container();

            //Instantiate the NotifyIcon attaching it to the components container and 
            //provide it an icon, note, you can imbed this resource 
            notifyIcon = new NotifyIcon(components);
            notifyIcon.Icon = new System.Drawing.Icon("WallpaperDeleter.ico");
            notifyIcon.Text = "Wallpaper Deleter";
            notifyIcon.Visible = true;

            //Instantiate the context menu and items
            ContextMenuStrip contextMenu = new ContextMenuStrip();

            //Attach the menu to the notify icon
            notifyIcon.ContextMenuStrip = contextMenu;

            //Setup the items and add them to the menu strip, adding handlers to be created later
            ToolStripMenuItem deleteWallpaper = new ToolStripMenuItem();
            deleteWallpaper.Text = "Delete Wallpaper";
            deleteWallpaper.Click += new EventHandler(deleteWallpaper_Click);
            contextMenu.Items.Add(deleteWallpaper);

            ToolStripMenuItem exitApplication = new ToolStripMenuItem();
            exitApplication.Text = "Exit";
            exitApplication.Click += new EventHandler(exitApplication_Click);
            contextMenu.Items.Add(exitApplication);
        }

        /// <summary>
        /// Event handler to delete a wallpaper
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void deleteWallpaper_Click(object sender, EventArgs e)
        {
            // get original image path from system registry
            RegistryKey backgroundImageKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Desktop\General");
            string backgroundImagePath = backgroundImageKey.GetValue("WallpaperSource").ToString();

            // file deletion is confirmed by FileSystem.DeleteFile
            // file is moved to recycle bin
            try
            {
                FileSystem.DeleteFile(backgroundImagePath, UIOption.AllDialogs, RecycleOption.SendToRecycleBin);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            // change to next wallpaper.
        }

        private void exitApplication_Click(object sender, EventArgs e)
        {
            ExitThreadCore();
        }

        protected override void ExitThreadCore()
        {
            notifyIcon.Visible = false; // should remove lingering tray icon
            base.ExitThreadCore();
        }
    }
}
